package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.BspsInspectTaskDetailMapper;
import cc.rengu.igas.bsps.common.dao.BspsInspectTaskInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsInspectTaskDetailMapperImpl;
import cc.rengu.igas.bsps.common.dao.impl.BspsInspectTaskInfoMapperImpl;
import cc.rengu.igas.bsps.common.entity.InspectTaskDetail;
import cc.rengu.igas.bsps.common.entity.InspectTaskInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.request.MchntInspectTaskQueryRequest;
import cc.rengu.igas.bsps.facade.response.MchntInspectTaskQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

/**
 * @author zhangxuran
 * @version 1.0
 * @date 2020/04/04
 */
public class MchntInspectTaskQueryService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntInspectTaskQueryRequest mchntInspectTaskQueryRequest = new MchntInspectTaskQueryRequest();
        ConvertUtil.convertOutput(mchntInspectTaskQueryRequest);
        return mchntInspectTaskQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        MchntInspectTaskQueryRequest mchntInspectTaskQueryRequest = (MchntInspectTaskQueryRequest) request;
        MchntInspectTaskQueryResponse mchntInspectTaskQueryResponse = new MchntInspectTaskQueryResponse();
        /*获取请求报文参数信息：用户标识，商户号*/
        String instId = mchntInspectTaskQueryRequest.getHeader().getInstId();
        String userId = mchntInspectTaskQueryRequest.getUserId();
        String mchntNo = mchntInspectTaskQueryRequest.getMchntNo();
        if (StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(mchntNo) || StringUtil.isEmptyOrNull(instId)) {
            rglog.error("请求参数校验必填字段失败！");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        BizResponse<MchntInspectTaskQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        /*根据商户号查询商户巡检任务:商户只有一个巡检任务，不能同时并存多个巡检任务,线上巡检方式且状态为巡检待提交和巡检拒绝*/
        BspsInspectTaskDetailMapper bspsInspectTaskDetailMapper = new BspsInspectTaskDetailMapperImpl();
        InspectTaskDetail inspectTaskDetail = bspsInspectTaskDetailMapper.selectBspsInspectTaskDetailByMchntNo(instId, mchntNo);
        if (null == inspectTaskDetail) {
            rglog.error("查询商户巡检任务详情信息失败！");
            bizResponse.setResult(mchntInspectTaskQueryResponse);
            return bizResponse;
        }
        /*根据巡检任务编号查询巡检任务信息*/
        BspsInspectTaskInfoMapper bspsInspectTaskInfoMapper = new BspsInspectTaskInfoMapperImpl();
        if (!StringUtil.isEmptyOrNull(inspectTaskDetail.getInspectTaskNo())) {
            InspectTaskInfo inspectTaskInfo = bspsInspectTaskInfoMapper.selectBspsInspectTaskInfofoByInspectTaskNo(instId, inspectTaskDetail.getInspectTaskNo());
            if (null == inspectTaskInfo) {
                rglog.error("查询商户巡检任务信息失败！");
                throw new BizException(RespCodeEnum.GET_INSPECT_TASK_INFO_ERROR.getRespCode(), RespCodeEnum.GET_INSPECT_TASK_INFO_ERROR.getRespDesc());
            }
            /*组装响应报文信息*/
            /*巡检任务编号赋值*/
            mchntInspectTaskQueryResponse.setInspectTaskNo(inspectTaskDetail.getInspectTaskNo());
            /*巡检任务描述（名称）赋值*/
            mchntInspectTaskQueryResponse.setInspectName(inspectTaskInfo.getInspectTaskDesc());
            /*巡检任务开始时间赋值*/
            mchntInspectTaskQueryResponse.setInspectBeginTime(inspectTaskDetail.getInspectBeginTime());
            /*巡检任务截止时间赋值*/
            mchntInspectTaskQueryResponse.setInspectEndTime(inspectTaskDetail.getInspectEndTime());
        }
        /* 组装应答信息 */

        bizResponse.setResult(mchntInspectTaskQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntInspectTaskQueryResponse mchntInspectTaskQueryResponse = (MchntInspectTaskQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntInspectTaskQueryResponse);
    }
}
